home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Utilities Professional 1-1500
/
Utilities Professional 1-1500 (1994)(WPD)[!].iso
/
12511500
/
var1273.dms
/
var1273.adf
/
AmiCDROM
/
cdrom.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-02
|
13KB
|
480 lines
/* cdrom.c:
*
* Basic interaction with the CDROM drive.
*
* ----------------------------------------------------------------------
* This code is (C) Copyright 1993 by Frank Munkert.
* All rights reserved.
* This software may be freely distributed and redistributed for
* non-commercial purposes, provided this notice is included.
* ----------------------------------------------------------------------
* History:
*
* 06-Dec-93 fmu New drive type DRIVE_SCSI_2.
* 09-Nov-93 fmu Added Select_XA_Mode.
* 23-Oct-93 fmu Open_CDROM now returns an error code that tell what
* went wrong.
* 09-Oct-93 fmu SAS/C support added.
* 03-Oct-93 fmu New buffering algorithm.
* 27-Sep-93 fmu Added support for multi-LUN devices.
* 24-Sep-93 fmu - SCSI buffers may now reside in fast or chip memory.
* - TD_CHANGESTATE instead of CMD_READ in Test_Unit_Ready
*/
#include <string.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#ifdef AZTEC_C
#include <pragmas/exec_lib.h>
#endif
#ifdef LATTICE
#include <pragmas/exec_pragmas.h>
#endif
#if defined (_DCC) && defined (REGISTERED)
#include <pragmas/exec_pragmas.h>
#endif
#include <devices/trackdisk.h>
#include <limits.h>
#include "cdrom.h"
int g_cdrom_errno;
void Determine_Drive_Type (CDROM *p_cd)
{
t_inquiry_data data;
char buf[33];
p_cd->drive_type = DRIVE_ANY;
if (!Inquire (p_cd, &data))
return;
if ((data.version & 0x7) >= 2)
p_cd->drive_type = DRIVE_SCSI_2;
if (strncmp (data.vendor, "TOSHIBA", 7))
return;
memcpy (buf, data.product, 32);
buf[32] = 0;
if (strstr (buf, "3401"))
p_cd->drive_type = DRIVE_TOSHIBA_3401;
}
CDROM *Open_CDROM (char *p_device, int p_scsi_id, int p_use_trackdisk,
int p_use_fast_mem, int p_std_buffers, int p_file_buffers)
{
CDROM *cd;
int i;
int bufs = p_std_buffers + p_file_buffers + 1;
g_cdrom_errno = CDROMERR_NO_MEMORY;
cd = AllocMem (sizeof (CDROM),
MEMF_PUBLIC | MEMF_CLEAR |
(p_use_fast_mem ? MEMF_FAST : MEMF_CHIP));
if (!cd)
return NULL;
cd->buffer_data = AllocMem (SCSI_BUFSIZE * bufs + 15,
MEMF_PUBLIC |
(p_use_fast_mem ? MEMF_FAST : MEMF_CHIP));
if (!cd->buffer_data) {
FreeMem (cd, sizeof (CDROM));
return NULL;
}
cd->buffers = AllocMem (sizeof (unsigned char *) * bufs, MEMF_PUBLIC);
if (!cd->buffers) {
FreeMem (cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (cd, sizeof (CDROM));
return NULL;
}
cd->current_sectors = AllocMem (sizeof (long) * bufs, MEMF_PUBLIC);
if (!cd->current_sectors) {
FreeMem (cd->buffers, sizeof (unsigned char *) * bufs);
FreeMem (cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (cd, sizeof (CDROM));
return NULL;
}
cd->last_used = AllocMem (sizeof (unsigned long) * p_std_buffers,
MEMF_PUBLIC | MEMF_CLEAR);
if (!cd->last_used) {
FreeMem (cd->current_sectors, sizeof (long) * bufs);
FreeMem (cd->buffers, sizeof (unsigned char *) * bufs);
FreeMem (cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (cd, sizeof (CDROM));
return NULL;
}
/* make the buffer quad-word aligned. This greatly helps
* performance on '040-powered systems with DMA SCSI
* controllers.
*/
cd->buffers[0] = (UBYTE *)(((ULONG)cd->buffer_data + 15) & ~15);
for (i=1; i<bufs; i++)
cd->buffers[i] = cd->buffers[0] + i * SCSI_BUFSIZE;
cd->port = CreateMsgPort ();
if (!cd->port) {
g_cdrom_errno = CDROMERR_MSGPORT;
FreeMem (cd->last_used, sizeof (unsigned long) * p_std_buffers);
FreeMem (cd->current_sectors, sizeof (long) * bufs);
FreeMem (cd->buffers, sizeof (unsigned char *) * bufs);
FreeMem (cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (cd, sizeof (CDROM));
return NULL;
}
cd->scsireq = CreateIORequest (cd->port, sizeof (struct IOStdReq));
if (!cd->scsireq) {
g_cdrom_errno = CDROMERR_IOREQ;
DeleteMsgPort (cd->port);
FreeMem (cd->last_used, sizeof (unsigned long) * p_std_buffers);
FreeMem (cd->current_sectors, sizeof (long) * bufs);
FreeMem (cd->buffers, sizeof (unsigned char *) * bufs);
FreeMem (cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (cd, sizeof (CDROM));
return NULL;
}
if (OpenDevice ((UBYTE *) p_device, p_scsi_id,
(struct IORequest *) cd->scsireq, 0)) {
g_cdrom_errno = CDROMERR_DEVICE;
DeleteIORequest (cd->scsireq);
DeleteMsgPort (cd->port);
FreeMem (cd->last_used, sizeof (unsigned long) * p_std_buffers);
FreeMem (cd->current_sectors, sizeof (long) * bufs);
FreeMem (cd->buffers, sizeof (unsigned char *) * bufs);
FreeMem (cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (cd, sizeof (CDROM));
return NULL;
}
for (i=0; i<bufs; i++)
cd->current_sectors[i] = -1;
cd->use_trackdisk = p_use_trackdisk;
/* The LUN is the 2nd digit of the SCSI id number: */
cd->lun = (p_scsi_id / 10) % 10;
cd->std_buffers = p_std_buffers;
cd->file_buffers = p_file_buffers;
/* 'tick' is incremented every time a sector is accessed. */
cd->tick = 0;
g_cdrom_errno = 0;
Determine_Drive_Type (cd);
return cd;
}
int Do_SCSI_Command (CDROM *p_cd, unsigned char *p_buf, long p_buf_length,
unsigned char *p_command, int p_length)
{
int bufs = p_cd->std_buffers + p_cd->file_buffers + 1;
p_cd->scsireq->io_Length = sizeof (struct SCSICmd);
p_cd->scsireq->io_Data = (APTR) &p_cd->cmd;
p_cd->scsireq->io_Command = HD_SCSICMD;
p_cd->cmd.scsi_Data = (UWORD *) p_buf;
p_cd->cmd.scsi_Length = p_buf_length;
p_cd->cmd.scsi_Flags = SCSIF_AUTOSENSE | SCSIF_READ;
p_cd->cmd.scsi_SenseData = (UBYTE *) p_cd->sense;
p_cd->cmd.scsi_SenseLength = 18;
p_cd->cmd.scsi_SenseActual = 0;
p_cd->cmd.scsi_Command = (UBYTE *) p_command;
p_cd->cmd.scsi_CmdLength = p_length;
p_command[1] |= p_cd->lun << 5;
DoIO ((struct IORequest *) p_cd->scsireq);
if (p_cd->cmd.scsi_Status) {
int i;
for (i=0; i<bufs; i++)
p_cd->current_sectors[i] = -1;
return 0;
} else
return 1;
}
int Read_From_Drive (CDROM *p_cd, unsigned char *p_buf, long p_buf_length,
long p_sector, int p_number_of_sectors)
{
static unsigned char cmd[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int bufs = p_cd->std_buffers + p_cd->file_buffers + 1;
#ifdef DEBUG_SECTORS
extern void *dbprintf (char *, ...);
if (p_number_of_sectors == 1)
dbprintf ("[%ld]", p_sector);
else
dbprintf ("[%ld-%ld]", p_sector, p_sector + p_number_of_sectors - 1);
#endif
if (p_cd->use_trackdisk) {
p_cd->scsireq->io_Length = 2048 * p_number_of_sectors;
p_cd->scsireq->io_Data = (APTR) p_buf;
p_cd->scsireq->io_Offset = (ULONG) p_sector * 2048;
p_cd->scsireq->io_Command = CMD_READ;
DoIO ((struct IORequest *) p_cd->scsireq);
if (p_cd->scsireq->io_Error) {
int i;
for (i=0; i<bufs; i++)
p_cd->current_sectors[i] = -1;
return 0;
} else
return 1;
} else {
cmd[5] = p_sector & 0xff;
cmd[4] = (p_sector >> 8) & 0xff;
cmd[3] = (p_sector >> 16) & 0x1f;
cmd[8] = p_number_of_sectors;
return Do_SCSI_Command (p_cd, p_buf, p_buf_length, cmd, sizeof (cmd));
}
}
/* Read one sector from the CDROM drive.
*/
int Read_Sector (CDROM *p_cd, long p_sector)
{
int status;
int i;
int maxbuf = p_cd->std_buffers;
int loc;
p_cd->tick++;
for (i=0; i<maxbuf; i++)
if (p_cd->current_sectors[i] == p_sector) {
p_cd->buffer = p_cd->buffers[i];
p_cd->last_used[i] = p_cd->tick;
return 1;
}
/* find an empty buffer position: */
for (loc=0; loc<maxbuf; loc++)
if (p_cd->current_sectors[loc] == -1)
break;
if (loc==maxbuf) {
/* no free buffer position; remove the buffer that is unused
for the longest time: */
unsigned long oldest_tick = ULONG_MAX;
unsigned long tick;
for (loc=0, i=0; i<maxbuf; i++) {
tick = p_cd->last_used[i];
if (tick < oldest_tick)
loc = i, oldest_tick = tick;
}
}
status = Read_From_Drive (p_cd, p_cd->buffers[loc], SCSI_BUFSIZE,
p_sector, 1);
if (status) {
p_cd->current_sectors[loc] = p_sector;
p_cd->buffer = p_cd->buffers[loc];
p_cd->last_used[loc] = p_cd->tick;
}
return status;
}
/* Read_Contiguous_Sectors uses the 'file buffers' instead of the
* 'standard buffers'. Additionaly, more than one sector may be read
* with a single SCSI command. This may cause a substantial increase
* in speed when reading large files.
*/
int Read_Contiguous_Sectors (CDROM *p_cd, long p_sector, long p_last_sector)
{
int status;
int i;
int maxbuf = p_cd->std_buffers + p_cd->file_buffers;
int len;
for (i=p_cd->std_buffers; i<maxbuf; i++)
if (p_cd->current_sectors[i] == p_sector) {
p_cd->buffer = p_cd->buffers[i];
return 1;
}
if (p_last_sector <= p_sector)
len = 1;
else {
len = p_last_sector - p_sector + 1;
if (len > p_cd->file_buffers)
len = p_cd->file_buffers;
}
status = Read_From_Drive (p_cd, p_cd->buffers[p_cd->std_buffers],
SCSI_BUFSIZE * len, p_sector, len);
if (status) {
long sector = p_sector;
for (i=p_cd->std_buffers; len; i++, len--)
p_cd->current_sectors[i] = sector++;
p_cd->buffer = p_cd->buffers[p_cd->std_buffers];
}
return status;
}
int Test_Unit_Ready (CDROM *p_cd)
{
if (p_cd->use_trackdisk) {
p_cd->scsireq->io_Command = TD_CHANGESTATE;
if (!DoIO ((struct IORequest *) p_cd->scsireq)) {
if (!p_cd->scsireq->io_Actual)
return 1;
}
return 0;
} else {
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
static unsigned char cmd[6] = { 0, 0, 0, 0, 0, 0 };
return Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 6);
}
}
int Is_XA_Mode_Disk (CDROM *p_cd)
{
static unsigned char cmd[10] = { 0xC7, 3, 0, 0, 0, 0, 0, 0, 0, 0 };
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
if (!Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 10))
return FALSE;
return *(p_cd->buffers[dummy_buf]) == 0x20;
}
int Select_XA_Mode (CDROM *p_cd, int p_on)
{
static unsigned char cmd[6] = { 0x15, 0x10, 0, 0, 12, 0 };
static unsigned char mode[12] = { 0, 0, 0, 8,
0, 0, 0, 0, 0, 0, 0x8, 0 };
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
if (p_cd->use_trackdisk || p_cd->drive_type != DRIVE_TOSHIBA_3401)
return FALSE;
mode[4] = p_on ? 0x81 : 0;
memcpy (p_cd->buffers[dummy_buf], mode, sizeof (mode));
return Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 6);
}
int Inquire (CDROM *p_cd, t_inquiry_data *p_data)
{
static unsigned char cmd[6] = { 0x12, 0, 0, 0, 96, 0 };
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
if (p_cd->use_trackdisk)
return FALSE;
if (!Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 6))
return FALSE;
memcpy (p_data, p_cd->buffers[dummy_buf], sizeof (*p_data));
return 1;
}
t_toc_data *Read_TOC (CDROM *p_cd, t_toc_header *p_toc_header)
{
static unsigned char cmd[10] = { 0x43, 0, 0, 0, 0, 0, 1, 8, 0, 0 };
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
if (p_cd->use_trackdisk)
return NULL;
if (!Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 10))
return NULL;
memcpy (p_toc_header, p_cd->buffers[dummy_buf], sizeof (*p_toc_header));
return (t_toc_data *) (p_cd->buffers[dummy_buf] + 4);
}
int Has_Audio_Tracks (CDROM *p_cd)
{
t_toc_header hdr;
t_toc_data *toc;
int i, len;
if (!(toc = Read_TOC (p_cd, &hdr)))
return FALSE;
len = hdr.length / 8;
for (i=0; i<len; i++) {
if (toc[i].track_number != 0xAA &&
!(toc[i].flags & 4))
return toc[i].track_number;
}
return FALSE;
}
int Start_Play_Audio (CDROM *p_cd)
{
static unsigned char cmd[10] = { 0x48, 0, 0, 0, 0, 1, 0, 0, 1, 0 };
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
if (p_cd->use_trackdisk || p_cd->drive_type == DRIVE_ANY)
return FALSE;
cmd[4] = Has_Audio_Tracks (p_cd);
cmd[7] = cmd[4];
return Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 10);
}
int Stop_Play_Audio (CDROM *p_cd)
{
static unsigned char cmd[6] = { 0x1B, 0, 0, 0, 0, 0 };
int dummy_buf = p_cd->std_buffers + p_cd->file_buffers;
if (p_cd->use_trackdisk || p_cd->drive_type == DRIVE_ANY)
return FALSE;
return Do_SCSI_Command (p_cd, p_cd->buffers[dummy_buf], SCSI_BUFSIZE,
cmd, 6);
}
void Cleanup_CDROM (CDROM *p_cd)
{
int bufs = p_cd->std_buffers + p_cd->file_buffers + 1;
CloseDevice ((struct IORequest *) p_cd->scsireq);
DeleteIORequest (p_cd->scsireq);
DeleteMsgPort (p_cd->port);
FreeMem (p_cd->last_used, sizeof (unsigned long) * p_cd->std_buffers);
FreeMem (p_cd->current_sectors, sizeof (long) * bufs);
FreeMem (p_cd->buffers, sizeof (unsigned char *) * bufs);
FreeMem (p_cd->buffer_data, SCSI_BUFSIZE * bufs + 15);
FreeMem (p_cd, sizeof (CDROM));
}